﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using FrienDev.Api;
using FrienDev.Data;
using FrienDev.BusinessRules;
using System.Collections.Generic;

public partial class Controls_Club_MyClubInfo : System.Web.UI.UserControl
{
    int m_ClubId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        m_ClubId = GetQueryStringValue("clubId", 0);
        IEnumerable<ClubMember> m_ClubInfo = ClubRules.Instance.GetCurrentUserInfo(m_ClubId);
        if (m_ClubInfo.Count() > 0)
        {
            ClubMember cm = m_ClubInfo.First();
            this.hy_UserName.Text = cm.User.FullName;
            this.cimg_User.Attributes["src"] = ApiHelper.Instance.GetUserLogoUrl(cm.UserId);
            this.lbl_UserStatus.Text = cm.ClubMemberType.Name;
            this.hy_Jump.Visible = false;
            if (cm.ClubMemberTypeId <= ClubRules.m_3LevelMemberTypeId)
            {
                addAct.Visible = true;
                addAct.NavigateUrl = addAct.NavigateUrl + "?clubid=" + m_ClubId;
                inviteLink.Visible = true;
                inviteLink.NavigateUrl = ClubRules.Instance.GetClubInvitePageUrl(m_ClubId.ToString());
            }
        }
        else
        {
            User curUser = UserRules.Instance.GetCurrentUser();
            this.hy_UserName.Text = curUser.FullName;
            this.cimg_User.Attributes["src"] = ApiHelper.Instance.GetUserLogoUrl(curUser.UserId);
            this.lbl_UserStatus.Text = "非本俱乐部成员";
            this.hy_Jump.Text = "加入";
            this.hy_Jump.Visible = true;
            hy_Jump.Click += new EventHandler(hy_Jump_Click);
            //this.hy_Jump.NavigateUrl = "/Home/default.aspx";
        }
        ClubRules.Instance.AddVisitor(UserRules.Instance.GetCurrentUserId(), m_ClubId);
               
    }

    void hy_Jump_Click(object sender, EventArgs e)
    {
        if (!ClubRules.Instance.GetClubByClubId(m_ClubId).AddRule)
        {
            ClubRules.Instance.AddClubMember(m_ClubId, UserRules.Instance.GetCurrentUserId(), DateTime.Now, ClubRules.m_3LevelMemberTypeId);
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "javascript:alert('已成功加入该俱乐部');", true);
        }
        else
        {
            ClubRules.Instance.AddClubMember(m_ClubId, UserRules.Instance.GetCurrentUserId(), DateTime.Now, ClubRules.m_4LevelMemberTypeId);
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "javascript:alert('已递交申请,请等待批准');", true);
            //remind.Visible = true;
        }
        Response.Redirect("/Club/Detail.aspx?clubId=" + m_ClubId);
    }

    

    #region Protected Method
    protected int GetQueryStringValue(string QueryName, int nullValue)
    {

        if (string.IsNullOrEmpty(this.Page.Request.QueryString[QueryName]))
        {
            return nullValue;
        }
        return Convert.ToInt32(this.Page.Request.QueryString[QueryName]);
    }
    protected string GetQueryStringValue(string QueryName, string nullValue)
    {

        if (string.IsNullOrEmpty(this.Page.Request.QueryString[QueryName]))
        {
            return nullValue;
        }
        return this.Page.Request.QueryString[QueryName];
    }

    protected bool GetQueryStringValue(string QueryName, bool nullValue)
    {
        if (string.IsNullOrEmpty(this.Page.Request.QueryString[QueryName]))
        {
            return nullValue;
        }
        return Convert.ToBoolean(this.Page.Request.QueryString[QueryName]);
    }
    #endregion
}
